package offer;



/**
 * @Author Elephas
 * @Date 2022/1/18
 **/
public class HasPathSum {
    public boolean hasPathSum(TreeNode root, int targetSum){
        if(root == null){
            return false;
        }else{
            return hasPathSumHelper(root,0,targetSum);
        }
    }
    public boolean hasPathSumHelper(TreeNode root, int curSum, int targetSum){
        if(root.left != null && root.right != null){
            return hasPathSumHelper(root.left , curSum + root.val,targetSum)
                    || hasPathSumHelper(root.right, curSum + root.val, targetSum);
        }else if(root.left != null){
            return hasPathSumHelper(root.left , curSum + root.val,targetSum);
        }else if(root.right != null){
            return hasPathSumHelper(root.right, curSum + root.val, targetSum);
        }else{
            return curSum + root.val == targetSum;
        }

    }
}
